home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / e_mac / uio.c < prev   
Encoding:
C/C++ Source or Header  |  1997-05-22  |  5.8 KB  |  282 lines  |  [TEXT/CWIE]

  1. #include <Dialogs.h>
  2. #include <Events.h>
  3. #include <Files.h>
  4. #include <Stdlib.h>
  5. #include <Types.h>
  6. #include <Resources.h>
  7. #include <console.h>
  8. #include "ui.h"
  9.  
  10. #if defined( THINK_C ) || defined ( __MWERKS__ )
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <strings.h>
  14. #include "os_mac_eventchk.h"
  15. #ifdef THINK_C
  16. #include <fcntl.h>
  17. #include <stat.h>
  18. #else
  19. #include <unix.h>
  20. #endif
  21. #endif
  22.  
  23. #include <Gestalt.h>
  24.  
  25. char *get_wd_name (short wd_refnum, char *postfix)
  26. {
  27. #if defined( THINK_C ) || defined( __MWERKS__ )
  28.   unsigned char buf [64];
  29. #else
  30.   char buf [64];
  31. #endif
  32.   char *temp, *result;
  33.   DirInfo cinfo;
  34.   WDPBRec wd;
  35.  
  36.   wd.ioCompletion = NULL;
  37.   wd.ioNamePtr = NULL;
  38.   wd.ioVRefNum = wd_refnum;
  39.   wd.ioWDIndex = 0;
  40.   wd.ioWDProcID = 0;
  41.   wd.ioWDVRefNum = 0;
  42.   if( PBGetWDInfoSync( &wd ) != noErr ) return postfix;
  43.   cinfo.ioDrParID = wd.ioWDDirID;
  44.   result = malloc (strlen (postfix) + 1);
  45.   if (result == NULL) return postfix;
  46.   strcpy (result, postfix);
  47.   do{
  48.     cinfo.ioCompletion = NULL;
  49.     cinfo.ioNamePtr = buf;
  50.     cinfo.ioVRefNum = wd.ioWDVRefNum;
  51.     cinfo.ioFDirIndex = -1;
  52.     cinfo.ioDrDirID = cinfo.ioDrParID;
  53.     if (PBGetCatInfoSync ((CInfoPBPtr) &cinfo) != noErr) return postfix;
  54. #ifdef THINK_C
  55.     temp = malloc (strlen (result) + buf[0] + 2);
  56. #else
  57.     p2cstr (buf);
  58.     temp = malloc (strlen (result) + strlen ((char *)buf) + 2);
  59. #endif
  60.     if (temp == NULL){
  61.       free (result);
  62.       return postfix;
  63.     }
  64. #ifdef THINK_C
  65.     sprintf (temp, ":%#s%s", buf, result);
  66. #else
  67.     sprintf (temp, ":%s%s", buf, result);
  68. #endif
  69.     free (result);
  70.     result = temp;
  71.   }while (cinfo.ioDrDirID != 2);
  72.   return result + 1;
  73. }
  74.  
  75. #ifdef __MC68K__
  76. #if __MC68K__
  77. static void die( unsigned char *txt )
  78. {
  79.   InitGraf( &qd.thePort );
  80.   InitFonts();
  81.   InitWindows();
  82.   InitMenus();
  83.   TEInit();
  84.   InitDialogs( 0L );
  85.   InitCursor();
  86.   ParamText( txt, "\p", "\p", "\p" );
  87.   StopAlert( ok_alertID, 0L );
  88.   ExitToShell();
  89. }
  90. #endif
  91. #endif
  92.  
  93. extern int sub_main (int argc, char *argv []);
  94.  
  95. extern unsigned char *console_title;
  96.  
  97. extern char *image_name = 0;
  98.  
  99. /* CAMLRUN  10Jan95 e
  100.    0 for a command line interface to load any Caml link'd file
  101.    1 to load image named in string resource
  102. */
  103.  
  104. #define CAMLRUN 1
  105.  
  106. #ifdef THINK_C
  107. extern WindowPeek cflush(FILE *fp);
  108. #endif
  109.  
  110. static long probe_image( char *name ) // returns length or -1
  111. {
  112.   long len = -1;
  113.   int fd = open( name, O_RDONLY | O_BINARY );
  114.   if (fd != -1)
  115.   {
  116.     len = lseek( fd, 0, SEEK_END );
  117.     close( fd );
  118.   }
  119.   return len;
  120. }
  121.  
  122. #define MIN_IMAGE_SIZE 1200
  123.  
  124. main ()
  125. {
  126.   int argc, fd;
  127.   char **argv;
  128.   WDPBRec wd;
  129.   char *args[6];
  130.   KeyMap kMap;
  131.   long len;
  132.   Str255 pstr;
  133.  
  134.   if ( GetApplLimit() > ( LMGetCurStackBase() - 40000 ) )
  135.   { SetApplLimit ( LMGetCurStackBase() - 40000 );
  136.   }
  137.  
  138. #ifdef __MC68K__
  139. #if __MC68K__
  140. #if __MC68020__
  141.   if ( /* NG on Sys 7.1 and others? -- 08Oct96 e
  142.           Gestalt( gestaltNativeCPUtype, &len ) || len < gestaltCPU68020
  143.        */
  144.        Gestalt( gestaltProcessorType, &len ) || len < gestalt68020
  145.      )
  146.   { die( "\pAn M68020 or better processor is required to run this application!" );
  147.   }
  148. #endif
  149. #if __MC68881__
  150.   if ( Gestalt( gestaltFPUType, &len ) || len == gestaltNoFPU )
  151.   { die( "\pAn FPU (or M64040) is required to run this application!\
  152.  Try using the SANE version." );
  153.   }
  154. #endif
  155. #endif
  156. #endif
  157.  
  158.   /* 10Jan95 e -- data fork is now PPC code...
  159.   char apname[32];
  160.   unsigned char *CurApName = LMGetCurApName();
  161.   memcpy(apname,((char *)CurApName + 1),*((char *)CurApName));
  162.   apname[*((char *)CurApName)] = 0;
  163.   */
  164.  
  165. #if CAMLRUN
  166.  
  167.   wd.ioCompletion = NULL;
  168.   wd.ioNamePtr = NULL;
  169.   PBHGetVolSync( &wd );
  170.  
  171.   // first, see if we were launched with an image document
  172.   // to do that we need to init MacOS which is done by the console
  173.   GetIndString(pstr, 357, 5); /* console title */
  174.   if ( pstr[0] == 0 )
  175.     console_title = "\pMosml Console";
  176.   else
  177.     console_title = pstr;
  178. #ifdef THINK_C
  179.   cflush(stdin);
  180. #else
  181.   puts(" ");
  182.   fflush(stdout);
  183. #endif
  184.   for (fd=0; fd<20; fd++) os_event_check(); /* let any open apple events in */
  185.   /* arg 0 */
  186.   len = (image_name == NULL) ? 0 : probe_image( image_name );
  187.   if( len >= MIN_IMAGE_SIZE )
  188.   {
  189.     args[0] = image_name;
  190.   }
  191.   else
  192.   {
  193.     GetIndString(pstr, 357, 1); /* image name */
  194.     p2cstr(pstr);
  195.     args[0] = get_wd_name( wd.ioVRefNum, (char *)pstr );
  196.     len = probe_image( args[0] );
  197.   }
  198.   if( len >= 0 )
  199.   {
  200.     GetKeys( kMap );
  201.     // fd = ((len < 12000) || (kMap[1] & 4)) ? -1 : 0; -- option key
  202.     fd = ((len < MIN_IMAGE_SIZE) || (kMap[1] & 32768L)) ? -1 : 0;
  203.   } /* probe image, insure big enough, command key for command line */
  204.   else fd = -1;
  205.  
  206.   /* arg 1 */
  207.   args[1] = "-stdlib";
  208.  
  209.   /* arg 2 */
  210.   GetIndString(pstr, 357, 2); /* stdlib directory name */
  211.   p2cstr(pstr);
  212.   args[2] = get_wd_name( wd.ioVRefNum, (char *)pstr );
  213.  
  214.   argc = 3;
  215.  
  216.   /* optional arg 3 */
  217.   GetIndString(pstr, 357, 3);     /* options, e.g., -P */
  218.   if ( pstr[0] != 0 )
  219.   {
  220.     args[3] = malloc( pstr[0] );
  221.     if( args[3] != NULL )
  222.     { argc = 4;
  223.       p_to_c( pstr, args[3] );
  224.       /* optional arg 4 */
  225.       GetIndString(pstr, 357, 4); /* options, e.g., {full,none,etc.} */
  226.       if ( pstr[0] != 0 )
  227.       {
  228.         args[4] = malloc( pstr[0] );
  229.         if( args[4] != NULL )
  230.         { argc = 5;
  231.           p_to_c( pstr, args[4] );
  232.         }
  233.       }
  234.     }
  235.   }
  236.  
  237. #else
  238.  
  239.   fd = -1;
  240.  
  241. #endif
  242.  
  243.   if ( fd != -1 )
  244.   { 
  245.     argv = args;
  246.     argv[argc] = NULL;
  247. /* moved above
  248. #ifdef THINK_C
  249.     cflush(stdin);
  250. #else
  251.     puts(" ");
  252.     fflush(stdout);
  253. #endif
  254. */
  255.   }
  256.   else
  257.   {
  258.     if ( 1 /* kMap[1] & 1 */  ) // shift key
  259.     {
  260.       fputs(args[0],stdout);
  261.       fputs(" ",stdout);
  262.       fputs(args[1],stdout);
  263.       fputs(" ",stdout);
  264.       fputs(args[2],stdout);
  265.       fputs(" ",stdout);
  266.       fputs(args[3],stdout);
  267.       fputs(" ",stdout);
  268.        puts(args[4]);
  269.     }
  270.     argc = ecommand( (unsigned char ***)&argv );
  271.   }
  272.  
  273.   if( argc > 0 ) // ecommand can do this now -- 13May97 e
  274.   {
  275.     init_timers();
  276.     caml_main( argc, argv );
  277.   }
  278.   return 0;
  279. }
  280.  
  281. // end of uio.c
  282.